/*
 * StateMachine.h
 *
 *  Created on: Apr 3, 2013
 *      Author: root
 */

#ifndef STATEMACHINE_H_
#define STATEMACHINE_H_

#include <opencv/cv.h>
#include <opencv/highgui.h>

#include "State.h"
#include "Buffer.h"
#include "GlobalVars.h"

class StateMachine{

	State* CurrentState;
	State* PreviousState;
	State* GlobalState;
	GlobalVars* Vars;
	Buffer* m_Buffer;
	CvCapture* cap;

	bool Run;
public:
	StateMachine():CurrentState(0),PreviousState(0),GlobalState(0),Run(true){
		Vars = new GlobalVars();
		m_Buffer = new Buffer();
		cap = cvCreateCameraCapture(-1);
		m_Buffer->checkStart();
		m_Buffer->StartUp();
	}

	void setCurrentState(State* s){CurrentState = s;}
	void setGlobalState(State* s){GlobalState = s;}
	void StopRun(){Run = false;}
	bool getRun(){return Run;}
	void Update(){
		if(Run){
		if(CurrentState)
			CurrentState->Execute(this,m_Buffer,Vars,cap);
		if(GlobalState)
			GlobalState->Execute(this,m_Buffer,Vars,cap);
		}
	}
	void changeState(State* s){
		CurrentState->Exit();
		PreviousState = CurrentState;
		CurrentState = s;
		CurrentState->Enter();
	}
	void revertState(){
		CurrentState->Exit();
		CurrentState = PreviousState;
		CurrentState->Enter();
	}


};


#endif /* STATEMACHINE_H_ */
